const workService = require('../services/workService')

const getWork = async (req, res) => {
  try {
    const result = await workService.getWork(req.query)
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const getLastWork = async (req, res) => {
  try {
    const result = await workService.getLastWork(req.query)
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const saveWork = async (req, res) => {
  try {
    await workService.saveWork(req.body)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const hasFinished = async (req, res) => {
  try {
    await workService.hasFinished(req.body.id)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const allFinished = async (req, res) => {
  try {
    const result = await workService.allFinished(req.query.date)
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

module.exports = {
  getLastWork,
  getWork,
  saveWork,
  hasFinished,
  allFinished
}
